<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 面包屑导航区 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
      </el-breadcrumb>

      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            clearable
            v-model="queryInfo.query">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchAdmin">
            </el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 管理员表格区 -->
      <el-table
        :header-cell-style="{textAlign: 'center'}"
        :cell-style="{ textAlign: 'center' }"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="序号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="adminname"
          label="姓名"
          width="180">
        </el-table-column>
        <el-table-column
          prop="password"
          label="密码"
          width="180">
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="手机号码">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button 
              type="primary" 
              icon="el-icon-edit" 
              size="mini"
              @click="getAdminInfo(scope.row)"></el-button>
            <el-button 
              type="danger" 
              icon="el-icon-delete" 
              size="mini" 
              @click="delAdmin(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination>
    </el-card>

    <!-- 修改管理员信息对话框 -->
    <el-dialog
      title="修改管理员信息"
      :visible.sync="editDialogVisible"
      width="50%">
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="80px">
        <el-form-item label="序号" prop="id">
          <el-input v-model="editForm.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="管理员名" prop="adminname">
          <el-input v-model="editForm.adminname"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="editForm.mobile"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAdmin">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getAllAdmin()
  },
  data() {
    return {
      // 获取所有管理员的查询参数
      queryInfo: {
        pageNum: 1,
        pageSize: 10,
        query: ""
      },
      total: 0,
      // 管理员数据
      tableData: [],
      // 修改管理员数据
      editDialogVisible: false,
      editForm: {
        adminname: '',
        mobile: '',
        password: '',
        id: ''
      },
      editFormRules: {
        adminname: [
          { required: true, message: '用户名是必填项', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号码是必填项', trigger: 'blur' },
          { min: 11, max: 14, message: '请输入正确格式的手机号码', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码是必填项', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getAllAdmin() {
      const { data: res } = await this.$http.post('allAdminLogin', this.queryInfo)
      if (res.code != 'success') return this.$message.error('获取管理员列表失败')
      this.tableData = res.data
      this.total = res.numbers
      // console.log(this.tableData)
      // console.log(this.total)
    },
    getAdminInfo(admin) {
      this.editForm.adminname = admin.adminname
      this.editForm.mobile = admin.mobile
      this.editForm.password = admin.password
      this.editForm.id = admin.id
      this.editDialogVisible = true
    },
    async delAdmin(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('deleteAdmin', { id: id })
      if (res !== 'success') return this.$message.error('管理员删除失败')
      this.$message.success('管理员删除成功')
      this.getAllAdmin()
    },
    editAdmin() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('updateAdmin', this.editForm)
        if (res !== 'success') return this.$message.error('修改管理员信息失败')
        this.$message.success('修改管理员信息成功')
        this.editDialogVisible = false
        this.getAllAdmin()
      })
    },
    async searchAdmin() {
      const { data: res } = await this.$http.post('allAdminLogin', this.queryInfo)
      if (res.code !== 'success') return this.$message.error('管理员查询失败')
      this.$message.success('管理员查询成功')
      this.tableData = res.data
    },
    // 页码发生改变时
    handleSizeChange() {},
    // 当前页数发生改变时
    handleCurrentChange() {}
  }
}
</script>
